package sweezy.swf.tools.abcdump
{
	import flash.utils.Dictionary;

	public class StringDumpStream implements IDumpStream
	{

		private static var _indentCache:Dictionary= new Dictionary();

		protected var _indent:String;

		private var _buffer:Vector.<String>;

		private var _context:Object;

		private var _indentLevel:int;

		private var _index:int;

		public function StringDumpStream()
		{
			_indent = "";
			_index = 0;
			_buffer = new Vector.<String>();
			_context = {};
		}

		public function get context():Object
		{
			return _context;
		}

		public function get indentLevel():int
		{
			return _indentLevel;
		}
		
		protected var indentString:String = "  ";

		public function set indentLevel(value:int):void
		{
			if (value < 0)
			{
				value = 0;
			}

			_indentLevel = value;
			var indent:String = _indentCache[value];
			if (indent === null)
			{
				indent = "";
				for (var i:int = 0; i < value; i++)
				{
					indent += indentString;
				}
				_indentCache[value] = indent;

			}
			_indent = indent;
		}

		public function printSection(value:String = ""):void
		{
			println("////////////////////////////////////////////////////");
			println("//" + value);
			println("////////////////////////////////////////////////////");
		}

		public function println(value:String = ""):void
		{
			_buffer[_index++] = _indent;
			_buffer[_index++] = value;
			_buffer[_index++] = "\n";
		}

		public function toString():String
		{
			return _buffer.join("");
		}
	}
}
